package org.xiaov.core.collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/3/9 23:02
 */
public class ListHelper {
    private static final Logger log = LoggerFactory.getLogger(ListHelper.class);

    /**
     * 新建一个List
     *
     * @param <T>
     * @return
     */
    public static <T> List<T> newArrayList() {
        return new ArrayList<>();
    }

    /**
     * 新建一个List
     *
     * @param collection
     * @param <T>
     * @return
     */
    public static <T> List<T> newArrayList(Collection<T> collection) {
        return new ArrayList<>(collection);
    }

    /**
     * 新建一个List，并初始化参数
     *
     * @param t
     * @param <T>
     * @return
     */
    @SafeVarargs
    public static <T> List<T> newArrayList(T... t) {
        return newArrayList(Arrays.asList(t));
    }

    /**
     * 将list转化成以{@code separator}分割的字符串
     *
     * @param list
     * @param separator
     * @param <T>
     * @return
     */
    public static <T> String toSubString(List<T> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                sb.append(list.get(i));
            } else {
                sb.append(list.get(i));
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    /**
     * 将list转化成以逗号分割的字符串
     *  默认为，
     * @param list
     * @param <T>
     * @return
     */
    public static <T> String toSubString(List<T> list) {
        return toSubString(list, ",");
    }
}

